var domUtil = require('wind-dom')

import { triggerNativeEvent } from './event'

var support = {}
const vendors = ['t', 'webkitT', 'MozT', 'msT', 'OT']
const transitionEventNames = {
  'WebkitTransition': 'webkitTransitionEnd',
  'MozTransition'   : 'transitionend',
  'OTransition'     : 'OTransitionEnd otransitionend',
  'transition'      : 'transitionend'
}

const style = document.createElement('div').style

const _vendor = (function() {
  for (let prefix of vendors) {
    let transform = prefix + 'ransform'
    if (transform in style) return prefix.slice(0, -1)
  }

  return false
}())

const transition = (function() {
  for (let name in transitionEventNames) {
    if (style[name] !== undefined) {
      return { end: transitionEventNames[name] }
    }
  }
}())
support.transition = transition

const emulateTransitionEnd = function(el, duration) {
  let called = false
  domUtil.on(el, transition.end, function() { called = true })
  setTimeout(function() {
    if (!called) triggerNativeEvent(el, transition.end)
  }, duration)
}

export {
  support as support,
  emulateTransitionEnd as emulateTransitionEnd
}